package com.jy.rhin.infra.repo.notfication.subscribe.query;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.framework.base.entity.Page;
import com.jy.rhin.domain.notification.subscribe.entity.EnumSubscribeStatus;
import com.jy.rhin.infra.repo.notfication.subscribe.mapper.SubscribeMapper;
import com.jy.rhin.infra.repo.notfication.subscribe.query.dto.SubscribeFullDto;
import com.jy.rhin.sdk.dto.notification.req.IdDto;
import com.jy.rhin.sdk.dto.notification.req.SubscribePageReqDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeDetailDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeInfoDto;
import com.jy.starter.repository.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: lww
 * @Date: 2022/6/30 10:45
 */
@Service
public class SubscribeQueryHandler {

    @Resource
    SubscribeMapper subscribeMapper;

    /**
     * 分页查询主题订阅列表
     *
     * @param dto
     * @return
     */
    public Page<SubscribeInfoDto> queryPage(SubscribePageReqDto dto) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(dto.getPublisherCode()), "b.publisher_code", dto.getPublisherCode())
                .eq(!StringUtils.isEmpty(dto.getSubscriberCode()), "a.subscriber_code", dto.getSubscriberCode())
                .and(!StringUtils.isEmpty(dto.getQueryText()), wrapper -> wrapper.or().like("a.subscribe_uid", dto.getQueryText())
                        .or().like("b.topic_name", dto.getQueryText()))
                .ne(!Boolean.TRUE.equals(dto.getCancel()), "a.status", EnumSubscribeStatus.CANCEL.name())
                .orderByDesc("a.create_time");
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Object> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(dto.getCurrent().intValue(), dto.getSize().intValue(), true);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<SubscribeInfoDto> pageResult = subscribeMapper.queryPage(page, queryWrapper);
        return PageUtils.page(pageResult);
    }

    /**
     * 查询主题订阅详情
     *
     * @param dto
     * @return
     */
    public SubscribeDetailDto queryDetail(IdDto dto) {
        List<SubscribeFullDto> subscribeFullDtoList = subscribeMapper.queryDetail(dto.getId());
        if (CollectionUtils.isEmpty(subscribeFullDtoList)) {
            return null;
        }
        SubscribeDetailDto subscribeDetailDto = subscribeFullDtoList.get(0).toDetailDto();

        subscribeDetailDto.setFilters(subscribeFullDtoList
                .stream()
                .filter(subscribeFullDto->StringUtils.isNotBlank(subscribeFullDto.getQueryExp()))
                .map(SubscribeFullDto::toFilterInfoDto)
                .collect(Collectors.toList()));
        return subscribeDetailDto;
    }
}
